package com.nikmesoft.android.referendumfriend.service;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import android.graphics.Bitmap;
import android.widget.ImageView;

import com.nikmesoft.android.referendumfriend.models.ItemHobies;
import com.nikmesoft.android.referendumfriend.models.ItemMyActivities;
import com.nikmesoft.android.referendumfriend.models.MyActivities;
import com.nikmesoft.android.referendumfriend.models.userhobies;

public class MyActivitiesHandler extends DefaultHandler {
	private final static String ERRORCODE_XML = "ErrorCode";
	private final static String ERRORCODE_ID_XML = "errorID";
	private final static String ERRORCODE_MSG_XML = "errorMsg";
	private final static String ID = "id";
	private final static String ITEM = "item";
	private final static String CONTENT = "content";
	private final static String ID_USER = "id_user";
	private final static String USERNAME_USER = "username_user";
	private final static String LIKE = "like";
	private final static String DISLIKE = "dislike";
	private final static String DATE = "date";
	private final static String ID_HOBBY = "id_hobby";
	private final static String NAME_HOBBY = "name_hobby";
	private final static String ISLIKE = "is_liked";
	
	
	private StringBuilder builder;
	private ItemMyActivities item=null;
	private ErrorCode errorCode;
	private MyActivities result=new MyActivities();

	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		super.characters(ch, start, length);
		builder.append(new String(ch, start, length));
	}

	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		super.endElement(uri, localName, qName);
		if (this.errorCode != null) {
			if (localName.equalsIgnoreCase(ERRORCODE_ID_XML)) {
				errorCode.setErrorID(builder.toString().trim());
			} else if (localName.equalsIgnoreCase(ERRORCODE_MSG_XML)) {
				errorCode.setErrorMsg(builder.toString().trim());
			}
		}else if (localName.equalsIgnoreCase(ID)) {
			
			item.setId(builder.toString().trim());
		}
		else if (localName.equalsIgnoreCase(CONTENT)) {
			item.setContent(builder.toString().trim());
		}
		else if (localName.equalsIgnoreCase(ID_USER)) {
			item.setId_user(builder.toString().trim());
		}
		else if (localName.equalsIgnoreCase(USERNAME_USER)) {
			item.setUsername_user(builder.toString().trim());
		}
		else if (localName.equalsIgnoreCase(LIKE)) {
			item.setLike(builder.toString().trim());
		}
		else if (localName.equalsIgnoreCase(DISLIKE)) {
			item.setDislike(builder.toString().trim());
		}
		else if (localName.equalsIgnoreCase(DATE)) {
			item.setDate(builder.toString().trim());
		}
		else if (localName.equalsIgnoreCase(ID_HOBBY)) {
			item.setId_hobby(builder.toString().trim());
		}
		else if (localName.equalsIgnoreCase(ISLIKE)) {
			item.setIslike(builder.toString().trim());
		}
		else if (localName.equalsIgnoreCase(NAME_HOBBY)) {
			item.setName_hobby(builder.toString().trim());
		}
		else if (localName.equalsIgnoreCase(ITEM)) {
			result.add(item);
		}
		builder.setLength(0);

	}

	@Override
	public void startDocument() throws SAXException {
		super.startDocument();
		builder = new StringBuilder();
	}

	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		super.startElement(uri, localName, qName, attributes);
		if (localName.equalsIgnoreCase(ERRORCODE_XML)) {
			this.errorCode = new ErrorCode();
		}
		else if (localName.equalsIgnoreCase(ITEM)) {
	    	   item=new ItemMyActivities();
			}
	}

	public MyActivities getResult() {
		return result;
	}

}
